package com.zhishu.snatch;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.zhishu.snatch.model.H2Urls;
import com.zhishu.snatch.system.h2.H2Server;
import com.zhishu.snatch.system.mq.RepairLocalMQ;
import com.zhishu.snatch.system.mq.SnatchMQConsumer;
import com.zhishu.snatch.system.queue.SnatchConsumer;
import com.zhishu.snatch.system.util.SpringContextHolder;

@SpringBootApplication
@ComponentScan(basePackages = "com.zhishu")
@EnableAspectJAutoProxy(exposeProxy = true)
public class StartApp {
	private static Logger logger = Logger.getLogger(StartApp.class);

	public static void main(String[] args) throws Exception {

		PropertyConfigurator.configure(StartApp.class.getClassLoader().getResource("").getPath() + "log4j.properties");
		ApplicationContext application = SpringApplication.run(StartApp.class, args);
		logger.info("##########spring boot 启动成功##########");
		SpringContextHolder.setApplicationContext(application);
		H2Server.startH2Server();
		Properties properties = new Properties();
		properties.put("url", "jdbc:h2:~/urls");
		properties.put("username", "sa");
		properties.put("password", "");
		properties.put("driver", "org.h2.Driver");
		properties.put("type", "com.alibaba.druid.pool.DruidDataSource");
		properties.put("platform", "h2");
		DataSource ds = DruidDataSourceFactory.createDataSource(properties);
		Dao h2dao = new NutDao(ds);
		if (!h2dao.exists(H2Urls.class)) {
			h2dao.create(H2Urls.class, true);
		}

		RepairLocalMQ local = new RepairLocalMQ();
		new SnatchConsumer().doStart(h2dao);
		new SnatchMQConsumer().doStart(h2dao);

		local.doStart(h2dao);

		logger.info("全部启动完毕");
	}
}
